package com.mytijian.openapi.adapter.dianping.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dianping.utils.DzOpenParamConvertUtil;
import com.mytijian.openapi.adapter.dianping.client.DzopenUtils;
import com.mytijian.openapi.adapter.dianping.consts.DzopenConsts;
import com.mytijian.openapi.adapter.dianping.exceptions.DzopenException;
import com.mytijian.openapi.adapter.dianping.param.DzCancelbookParam;
import com.mytijian.openapi.adapter.dianping.param.DzConsumestatusResult;
import com.mytijian.openapi.adapter.dianping.param.DzCounterParam;
import com.mytijian.openapi.adapter.dianping.param.DzModifybooktimeParam;
import com.mytijian.openapi.adapter.dianping.param.DzStartBookParam;
import com.mytijian.openapi.adapter.dianping.param.DzopenShopAuthParam;
import com.mytijian.openapi.adapter.dianping.result.DzCancelbookResult;
import com.mytijian.openapi.adapter.dianping.result.DzCosumestatusParam;
import com.mytijian.openapi.adapter.dianping.result.DzCounterResult;
import com.mytijian.openapi.adapter.dianping.result.DzModifybooktimeResult;
import com.mytijian.openapi.adapter.dianping.result.DzResult;
import com.mytijian.openapi.adapter.dianping.result.DzStartbookResult;
import com.mytijian.openapi.adapter.dianping.service.DzOpenConsumeService;
import com.mytijian.openapi.adapter.dianping.service.DzopenAdapterService;
import com.mytijian.openapi.adapter.dianping.service.DzopenCancelbookService;
import com.mytijian.openapi.adapter.dianping.service.DzopenCounterService;
import com.mytijian.openapi.adapter.dianping.service.DzopenModifybooktimeService;
import com.mytijian.openapi.adapter.dianping.service.DzopenShopAuthService;
import com.mytijian.openapi.adapter.dianping.service.DzopenStartbookService;

/**
 * 仅负责 调用正真服务和解析输入、组装输出，不涉及具体业务。
 * 
 * @author clozz
 *
 */
@Service
public class DzopenAdapterServiceImpl implements DzopenAdapterService {
	
	private Logger logger = LoggerFactory.getLogger(DzopenAdapterServiceImpl.class);

	@Autowired
	private DzopenStartbookService dzopenStartbookService;

	@Autowired
	private DzopenCancelbookService dzopenCancelbookService;

	@Autowired
	private DzopenCounterService dzopenCounterService;

	@Autowired
	private DzOpenConsumeService dzOpenConsumeService;

	@Autowired
	private DzopenModifybooktimeService dzopenModifybooktimeService;
	
	@Autowired
	private DzopenShopAuthService dzopenShopAuthService;
	
	@Value("${dzopen.signkey}")
	private String signkey;

	@Override
	public DzResult<?> startbook(Map<String, String> requestMap) {
		DzResult<?> result = null;
		logger.info("[美团点评] 调用下单接口 参数 {}", JSON.toJSONString(requestMap));
		try {
			DzopenUtils.verifySign(requestMap, signkey, DzopenConsts.SignFields.startbook);
			DzStartBookParam param = DzOpenParamConvertUtil.convert(requestMap, DzStartBookParam.class);

			DzStartbookResult bookResult = dzopenStartbookService.startbook(param);
			result = buildResut(200, "success", bookResult);
		} catch (DzopenException e) {
			e.printStackTrace();
			result = buildResut(Integer.valueOf(e.getErrorCode()), e.getErrorMsg(), null);
		} catch (Exception e) {
			e.printStackTrace();
			result = buildResut(501, "服务调用异常", null);
		}
		logger.info("[美团点评] 调用下单接口返回 {}", JSON.toJSONString(result));
		return result;
	}

	@Override
	public DzResult<?> cancelbook(Map<String, String> requestMap) {
		DzResult<?> result = null;
		logger.info("[美团点评] 调用撤单接口 参数 {}", JSON.toJSONString(requestMap));
		try {
			DzopenUtils.verifySign(requestMap, signkey,DzopenConsts.SignFields.cancelbook);
			DzCancelbookParam param = DzOpenParamConvertUtil.convert(requestMap, DzCancelbookParam.class);
			DzCancelbookResult cancelResult = dzopenCancelbookService.cancelbook(param);
			result = buildResut(200, "success", cancelResult);
		} catch (DzopenException e) {
			logger.error(e.getMessage());
			result = buildResut(Integer.valueOf(e.getErrorCode()), e.getErrorMsg(), null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result =  buildResut(501, "服务调用异常", null);
		}
		logger.info("[美团点评] 调用撤单接口返回 {}", JSON.toJSONString(result));
		return result;
	}

	@Override
	public DzResult<?> remaincountbytime(Map<String, String> requestMap) {
		DzResult<?> result = null;
		logger.info("[美团点评] 调用批量查询库存接口 参数 {}", JSON.toJSONString(requestMap));
		try {
			DzopenUtils.verifySign(requestMap, signkey, DzopenConsts.SignFields.remaincountbytime);
			DzCounterParam param = DzOpenParamConvertUtil.convert(requestMap, DzCounterParam.class);
			List<DzCounterResult> list = dzopenCounterService.getCounter(param);
			result = buildResut(200, "success", list);
		} catch (DzopenException e) {
			logger.error(e.getMessage());
			result = buildResut(Integer.valueOf(e.getErrorCode()), e.getErrorMsg(), null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result =  buildResut(501, "服务调用异常", null);
		}
		logger.info("[美团点评] 批量查询库存接口返回 {}", JSON.toJSONString(result));
		return result;
	}

	@Override
	public DzResult<?> consumestatus(Map<String, String> requestMap) {
		DzResult<?> result = null;
		logger.info("[美团点评] 调用查询核销接口 参数 {}", JSON.toJSONString(requestMap));
		try {
			DzopenUtils.verifySign(requestMap, signkey, DzopenConsts.SignFields.cosumestatus);
			DzCosumestatusParam param = DzOpenParamConvertUtil.convert(requestMap, DzCosumestatusParam.class);
			DzConsumestatusResult consumeResult = dzOpenConsumeService.queryOrderCosumestatus(param);
			result = buildResut(200, "success", consumeResult);
		} catch (DzopenException e) {
			logger.error(e.getMessage());
			result = buildResut(Integer.valueOf(e.getErrorCode()), e.getErrorMsg(), null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result = buildResut(501, "服务调用异常", null);
		}
		logger.info("[美团点评] 调用查询核销返回 {}", JSON.toJSONString(result));
		return result;
	}

	@Override
	public DzResult<?> modifybooktime(Map<String, String> requestMap) {
		DzResult<?> result = null;
		logger.info("[美团点评] 调用改期接口 参数 {}", JSON.toJSONString(requestMap));
		try {
			DzopenUtils.verifySign(requestMap, signkey, DzopenConsts.SignFields.modifybooktime);
			DzModifybooktimeParam param = DzOpenParamConvertUtil.convert(requestMap, DzModifybooktimeParam.class);
			DzModifybooktimeResult modifyResult = dzopenModifybooktimeService.modifybooktime(param);
			result = buildResut(200, "success", modifyResult);
		} catch (DzopenException e) {
			logger.error(e.getMessage());
			result = buildResut(Integer.valueOf(e.getErrorCode()), e.getErrorMsg(), null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result = buildResut(501, "服务调用异常", null);
		}
		logger.info("[美团点评] 调用改期接口返回 {}", JSON.toJSONString(result));
		return result;
	}



	@Override
	public DzResult<?> oauthcallback(Map<String, String> requestMap) {
		DzResult<?> result = null;
		try {
			requestMap.put("app_secret",signkey);
			DzopenUtils.verifySign(requestMap, signkey, DzopenConsts.SignFields.oauthcallback);
			
			DzopenShopAuthParam param = DzOpenParamConvertUtil.convert(requestMap, DzopenShopAuthParam.class);
			dzopenShopAuthService.doAuth(param);
			result =  buildResut(200, "success", true);
			
		} catch (DzopenException e) {
			logger.error(e.getMessage());
			result =  buildResut(Integer.valueOf(e.getErrorCode()), e.getErrorMsg(), null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result =  buildResut(501, "服务调用异常", null);
		}
		logger.info("[美团点评] 授权接口返回{} ",JSON.toJSONString(result));
		return result;
	}


	private <T> DzResult<T> buildResut(int code, String msg, T data) {
		DzResult<T> result = new DzResult<>();

		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);

		return result;
	}
}
